<!-- 1903010328 查询通行证 -->
<?php
include "../../inc/log/adsession.php";
// include "../../inc/data/connAdmin.php";
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
	require '../../vendor/autoload.php';
	include "../../inc/data/connAdmin.php";
	
	// 引入资源
	use PhpOffice\PhpSpreadsheet\IOFactory;

	//实例化phpspreadsheet对象
	$file = $_FILES["coufile"]["tmp_name"];
	$spreadSheet = IOFactory::load($file);

	//获取表
	$workSheet = $spreadSheet->getSheet(0);

	// 最大行数
	$rows = $workSheet->getHighestRow();
	
	for ($i=3; $i <= $rows; $i++) { 
		//  获取表格中每一列的信息
			$cCode = $workSheet->getCell("a$i")->getValue();
			$cName = $workSheet->getCell("b$i")->getValue();
			$cGrade = $workSheet->getCell("c$i")->getValue();
			$majorName = $workSheet->getCell("d$i")->getValue();
			$coterm = $workSheet->getCell("e$i")->getValue();
			$cPoint = $workSheet->getCell("f$i")->getValue();
			$cWeekh = $workSheet->getCell("g$i")->getValue();
			$cWeek = $workSheet->getCell("h$i")->getValue();
			$ctoTalh = $workSheet->getCell("i$i")->getValue();
			$cType = $workSheet->getCell("j$i")->getValue();
			$cExam = $workSheet->getCell("k$i")->getValue();
		$sql = "insert into mamx_course 
				(cocode,coname,cograde,majorname,coterm,copolint,coweekh,coweek,cototalh,cotype,coexam) 
				values ('$cCode','$cName','$cGrade','$majorName','$coterm','$cPoint','$cWeekh','$cWeek','$ctoTalh','$cType','$cExam')";
		if($stmt=$conn->prepare($sql)){
			// 获取学号,判断是否为空
			$sCode = $workSheet->getCell("a$i")->getValue();
			// 查看是否是有效值
			if ($sCode == ""){
				break;
			}
			$stmt->execute();
			if ($stmt->affected_rows > 0) {
				$count += 1;
			}
		}
	}
	if ($count > 0){
		//  录入成功弹出提示框提示
		echo <<<END
	    <script type="text/javascript">
	    alert("已成功添加 $count 个课程！");
	    history.back();
	    </script>
END;
	}else{
		//  录入失败弹出提示框提示
		?>
		<script type="text/javascript">
	    alert("添加失败，请重试！");
	    history.back();
	    </script>
	    <?php
	}
	?>
</body>
</html>